﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace medder
{
    public partial class ViewOrdersReg : Form
    {

        MedBaseEntities med = new MedBaseEntities();

        public static ObjectSet<Doctor> doctors;
        public static ObjectSet<TimeTable> timetables;
        public static ObjectSet<User> users;



        public ViewOrdersReg()
        {
            InitializeComponent();
            doctors = med.Doctors;
            timetables = med.TimeTables;
            users = med.Users;
        }

        private void ViewOrdersReg_Load(object sender, EventArgs e)
        {

            bookView.Columns.Add("Patient", "Пациент");
            bookView.Columns["Patient"].ReadOnly = true;
            bookView.Columns["Patient"].Width = 235;

            bookView.Columns.Add("Time", "Время");
            bookView.Columns["Time"].ReadOnly = true;
            bookView.Columns["Time"].Width = 80;

            foreach (var doc in doctors)
            {
                User docUser = users.Where(x => x.Passport.Equals(doc.Passport)).FirstOrDefault();
                comboBox1.Items.Add(string.Format("{0} {1} {2}", docUser.Surname, docUser.Name, docUser.Patronymic));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (var x in timetables)
            {
                if (!comboBox1.Text.Equals(String.Empty))
                {
                    int docId = comboBox1.SelectedIndex;
                    List<Doctor> usedDoctors = new List<Doctor>();
                    foreach (var xx in doctors)
                    {
                        usedDoctors.Add(xx);
                    }
                    Doctor doc = usedDoctors.ElementAt<Doctor>(docId);
                    FillTable(GetTable(doc));

                }
            }
        }

        private List<TimeTable> GetTable(Doctor usedDoctor)
        {
            DateTime date = dateTimePicker1.Value;

            List<TimeTable> timetable = new List<TimeTable>();

            foreach (var x in timetables)
            {

                    if ((x.DoctorID.Equals(usedDoctor.Passport)) && (x.Date.Value.ToShortDateString().Equals(date.ToShortDateString())))
                        timetable.Add(x);

            }
            return timetable;

        }

        private void FillTable(List<TimeTable> timeTable)
        {
            bookView.Rows.Clear();
            int i = 0;
            foreach (TimeTable table in timeTable)
            {
                User docUser = users.Where(x => x.Passport.Equals(table.PatientID)).FirstOrDefault();
                bookView.Rows.Add();
                bookView.Rows[i].Cells[0].Value = string.Format("{0} {1} {2}", docUser.Surname, docUser.Name, docUser.Patronymic);
                bookView.Rows[i].Cells[1].Value = Convert.ToDateTime(table.Time).ToShortTimeString();
                i++;
            }

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            comboBox1_SelectedIndexChanged(sender, e);
        }
    }
}
